//
//  DHSearchColorItem.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/21.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchColorItem.h"

@interface DHSearchColorItem()



@end

@implementation DHSearchColorItem

- (instancetype)initWithFrame:(CGRect)frame andColor:(NSString *)color
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/2 - 7, frame.size.height/2 - 7, 14, 14)];
        [colorView setBackgroundColor:[UIColor colorWithHexString:color]];
        colorView.layer.cornerRadius = 7;
        if ([color isEqualToString:@"#FFFFFF"]) {
            colorView.layer.borderWidth = 1;
            colorView.layer.borderColor = [[UIColor colorWithHexString:@"#979797"] CGColor];
        }
        colorView.userInteractionEnabled = NO;
        [self addSubview:colorView];
        
        [self addTarget:self action:@selector(selectColor:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)selectColor:(UIControl *)sender{
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(sender.width/2 - 18, sender.height/2 - 18, 36, 36)];
        bgImageView.tag = 1000;
        bgImageView.image = [UIImage imageNamed:@"color_bg"];
        bgImageView.layer.cornerRadius = 18;
        bgImageView.layer.masksToBounds = YES;
        bgImageView.alpha = 0;
        [sender insertSubview:bgImageView atIndex:0];
        [UIView animateWithDuration:0.2 animations:^{
            bgImageView.alpha = 1.0;
        }];
    }else{
        for (UIView *view in sender.subviews) {
            if (view.tag == 1000) {
                [view removeFromSuperview];
            }
        }
    }
}

@end
